package com.flyu.service.user;

import com.flyu.dao.BaseDao;
import com.flyu.dao.user.UserDao;
import com.flyu.dao.user.UserDaoImpl;
import com.flyu.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class UserServiceImpl implements UserService{

    // 业务层都会调用Dao层，所以引入UserDao
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }
    // 用户登录
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            // 通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        // 判断密码是否正确
        if (user.getUserPassword().equals(password)){
            return user;
        }
        return null;
    }
    // 修改密码
    public boolean updatePwd(int id, String password) {
        Connection connection = null;
        boolean flag = false;


        try {
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection, id, password)>0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return flag;
    }
    // 根据用户名或角色查询用户总数
    public int getUserCount(String username, int userRole) {
        Connection connection = null;
        int count = 0;

        try {
            connection = BaseDao.getConnection();
            count = userDao.getUserCount(connection, username, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return count;
    }
    // 获取用户列表
    public List<User> getUserList(String username, int userRole, int currentPage, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, username, userRole, currentPage, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }
    // 添加用户
    public boolean addUser(User user){
        boolean flag = false;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);    // 开启JDBC事务管理

            int updateRows = userDao.addUser(connection, user);
            connection.commit();
            if (updateRows > 0){
                flag = true;
                System.out.println("Add User sucess!");
            }else{
                System.out.println("Add User failed!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 判断userCode是否存在
    public boolean userCodeExist(String userCode) {
        boolean isExist = false;
        Connection connection = null;
        // 利用userCode去查询用户，如果有user返回则存在，否则不存在
        try {
            connection = BaseDao.getConnection();
            User user = userDao.getLoginUser(connection, userCode);
            if (user != null){  // 如果有返回值，说明userCode已存在
                isExist = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return isExist;
    }
    // 根据id删除用户
    public boolean delUser(int uid) {
        boolean flag = false;

        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = userDao.delUser(connection, uid);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }

        return flag;
    }
    // 根据id查找用户
    public User getUserById(int uid) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }
    // 保存修改
    public boolean modifySave(Object[] params) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int i = userDao.modifySave(connection, params);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }


    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        List<User> userList = userService.getUserList("test", 0, 1, 5);
        for (User user : userList) {
            System.out.println(user.getUserName());
        }
    }


}
